﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using azurecp;
using System.Diagnostics;
using System.Collections.Concurrent;
using System.Configuration;

namespace AzureCP_Console
{
    class Program
    {
        static void Main(string[] args)
        {
            TestQuery();
            //ResetLogging();
            //LockCollTest();
            //ConcurrentBagTest();
            Console.WriteLine("Finished");
            Console.ReadLine();
        }

        static void TestQuery()
        {
            var appSettings = ConfigurationManager.AppSettings;

            // Create PO in config DB
            AzureCPConfig config = AzureCPConfig.CreatePersistedObject();
            config.AzureTenants = new List<AzureTenant>
            {
                new AzureTenant{TenantName=appSettings["TenantName"], TenantId=appSettings["TenantId"], ClientId=appSettings["ClientId"], ClientSecret=appSettings["ClientSecret"]},
            };
            config.Update();

            AzureCP cp = new AzureCP(AzureCP._ProviderInternalName);
            bool initSuccess = cp.Initialize(null, null);
            if (!initSuccess) return;
            List<AzurecpResult> results = new List<AzurecpResult>();
            cp.BuildFilterAndProcessResults("user1@yvandoutlook.onmicrosoft.com", cp.ProcessedAzureObjects, true, null, new string[] { }, ref results);
            cp.BuildFilterAndProcessResults("user2@yvandoutlook.onmicrosoft.com", cp.ProcessedAzureObjects, true, null, new string[] { }, ref results);
            cp.BuildFilterAndProcessResults("user3@yvandoutlook.onmicrosoft.com", cp.ProcessedAzureObjects, true, null, new string[] { }, ref results);
            
            foreach(AzurecpResult result in results)
            {
                Console.WriteLine("value: {0}, claim type: {1}", result.PermissionValue, result.AzureObject.ClaimType);
            }
        }

        public static void ResetLogging()
        {
            AzureCPLogging.Unregister();
            AzureCPLogging loggingsvc = AzureCPLogging.Local;
        }

        private static void LockCollTest()
        {
            int collSize = 10000000;
            object list1_lock = new object();
            List<int> lst1 = new List<int>(collSize);

            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();

            Parallel.For(0, collSize, delegate(int i)
            {
                lock (list1_lock)
                {
                    lst1.Add(i);
                }
            });

            stopWatch.Stop();
            Console.WriteLine("Elapsed = {0}", stopWatch.Elapsed.TotalSeconds);
        }

        private static void ConcurrentBagTest()
        {
            int collSize = 10000000;
            Stopwatch stopWatch = new Stopwatch();
            ConcurrentBag<int> bag1 = new ConcurrentBag<int>();
            stopWatch.Start();

            Parallel.For(0, collSize, delegate(int i)
            {
                bag1.Add(i);
            });

            stopWatch.Stop();
            Console.WriteLine("Elapsed Time = {0}", stopWatch.Elapsed.TotalSeconds);
        }
    }
}
